package cn.wellwan.controller;

import cn.wellwan.commons.Result;
import cn.wellwan.entity.User;
import cn.wellwan.service.UserService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping(value = "list")
    public Result queryUserList(HttpServletRequest request) {

        User user = userService.findUserById(1L);
        if (user != null) {
            return Result.success().data(user);
        }
        return null;
    }

    @RequestMapping(value = "/first", method = RequestMethod.GET)
    public Result firstResp(HttpServletRequest request) {
        Result result = Result.success();
        request.getSession().setAttribute("request Url", request.getRequestURL());
        result.put("request Url", request.getRequestURL());
        return result;
    }

    @RequestMapping(value = "/sessions", method = RequestMethod.GET)
    public Result sessions(HttpServletRequest request) {
        Result result = Result.success();
        result.data("sessionId", request.getSession().getId());
        result.data("message", request.getSession().getAttribute("map"));
        return result;
    }

}
